﻿Imports Negocio
Imports Excepciones
Imports System.ComponentModel

Public Class TextBoxExt
    Inherits TextBox
    Implements IControlValidatable

#Region "Validacion"

    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero antes de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingBefore(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero luego de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingAfter(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    Private _validations As New List(Of Utilidades.ValidationType)
    <Browsable(True), _
    EditorBrowsable(EditorBrowsableState.Always), _
    Category("Validation"), _
    Description("Valida el texto con las opciones seleccionadas"), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public Property Validation() As List(Of Utilidades.ValidationType)
        Get
            Return _validations
        End Get
        Set(ByVal value As List(Of Utilidades.ValidationType))
            _validations = value
        End Set
    End Property

    Public Sub ValidateInput() Implements IControlValidatable.ValidateInput
        For Each tipo As Utilidades.ValidationType In _validations
            Utilidades.Validations.Validate(tipo, Me.Text)
        Next
    End Sub

    Protected Overridable Sub Me_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
        Dim frm As Base
        Try
            frm = DirectCast(Me.FindForm(), Base)
            frm.ResetErrorUI(sender)
            Try
                RaiseEvent ValidatingBefore(sender, e)
                ValidateInput()
                RaiseEvent ValidatingAfter(sender, e)
            Catch ex As CustomException
                e.Cancel = True
                frm.SetErrorUI(sender, ex)
            End Try
        Catch ex As Exception

        End Try

    End Sub

#End Region

End Class


'Public Class Sample

'    Private validtype As Utilidades.ValidationType

'    <Browsable(True), _
'    EditorBrowsable(EditorBrowsableState.Always), _
'    Category("SampleValid"), _
'    Description("Ejemplo"), _
'    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
'    Public Property SampleValid() As Utilidades.ValidationType
'        Get
'            Return validtype
'        End Get
'        Set(ByVal value As Utilidades.ValidationType)
'            validtype = value
'        End Set
'    End Property

'End Class